<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>平移实现定位居中</title>
  <style>
    div {
      position: absolute;
      left: 50%;
      top: 50%;

      /* 实现盒子向左向上移动自身尺寸的一半 */
      transform: translate(-50%, -50%);

      width: 200px;
      height: 100px;
      background-color: pink;
    }
  </style>
</head>
<body>
  <div class="box"></div>
</body>
</html>